package com.applink.sell_proof.database.table;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.Promotion;
import com.applink.sell_proof.util.Utility;

public class PromotionDBHelper {
	public static final String TABLE_NAME = "promotion_table";

	public static final String PM_ID = "PromotionID";
	public static final String PM_YEAR = "PromotionYear";
	public static final String PM_MONTH = "PromotionMonth";
	public static final String PM_CATEGORY = "PromotionCategory";
	public static final String PM_IMAGE_NAME = "PromotionImageName";
	public static final String PM_IMAGE_PATH = "PromotionImagePath";
	public static final String PM_IMAGE_PATH_LOCAL = "PromotionImageLocal";
	public static final String PM_DATETIME_SAVED = "PromotionDateTimeSaved";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ PM_ID + " integer primary key ," + PM_YEAR + " text ," + PM_MONTH
			+ " int ," + PM_CATEGORY + " text ," + PM_IMAGE_NAME + " text ,"
			+ PM_IMAGE_PATH + " text ," + PM_IMAGE_PATH_LOCAL + " text ,"
			+ PM_DATETIME_SAVED + " DATETIME);";

	public static void deleteAllPromotion() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		ArrayList<Promotion> promotions = getAllPromotion();
		if (promotions != null && promotions.size() > 0) {
			for (Promotion promotion : promotions) {
				int result = resolver.delete(
						EMarketingContentProvider.PROMOTION_CONTENT_URI, PM_ID
								+ "=?", new String[] { Long.toString(promotion
								.getAnnouncePromoID()) });
				if (result > 0) {
					Utility.deleteFile(promotion.getPhysicalPathLocal());
				}
			}
		}
	}

	public static void deletePromotion(int promotionId) {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		resolver.delete(EMarketingContentProvider.PROMOTION_CONTENT_URI, PM_ID
				+ "=?", new String[] { Integer.toString(promotionId) });
	}

	public static void createPromotion(final Promotion promotion) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues value = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				value.put(PM_ID, promotion.getAnnouncePromoID());
				value.put(PM_YEAR, promotion.getPromoYear());
				value.put(PM_MONTH, promotion.getPromoMonth());
				value.put(PM_CATEGORY, promotion.getSaleCategoryName());
				value.put(PM_IMAGE_NAME, promotion.getPromotionName());
				value.put(PM_IMAGE_PATH, promotion.getPhysicalPath());
				value.put(PM_IMAGE_PATH_LOCAL, promotion.getPhysicalPathLocal());
				value.put(PM_DATETIME_SAVED, promotion.getDownloadedDate());
				resolver.insert(
						EMarketingContentProvider.PROMOTION_CONTENT_URI, value);
			}
		}.start();
	}

	public static ArrayList<Promotion> getAllPromotion() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.PROMOTION_CONTENT_URI, null, null,
				null, PM_ID + " DESC ");

		if (cursor != null) {
			ArrayList<Promotion> promotions = new ArrayList<Promotion>();
			while (cursor.moveToNext()) {
				Promotion promotion = new Promotion();
				promotion.setAnnouncePromoID(cursor.getInt(cursor
						.getColumnIndex(PM_ID)));
				promotion.setPromoYear(cursor.getString(cursor
						.getColumnIndex(PM_YEAR)));
				promotion.setPromoMonth(cursor.getInt(cursor
						.getColumnIndex(PM_MONTH)));
				promotion.setSaleCategoryName(cursor.getString(cursor
						.getColumnIndex(PM_CATEGORY)));
				promotion.setPromotionName(cursor.getString(cursor
						.getColumnIndex(PM_IMAGE_NAME)));
				promotion.setPhysicalPath(cursor.getString(cursor
						.getColumnIndex(PM_IMAGE_PATH)));
				promotion.setPhysicalPathLocal(cursor.getString(cursor
						.getColumnIndex(PM_IMAGE_PATH_LOCAL)));
				promotions.add(promotion);
			}
			cursor.close();
			return promotions.size() > 0 ? promotions
					: new ArrayList<Promotion>();
		}
		return new ArrayList<Promotion>();
	}

	public static boolean isPromotionExists(int promotionId) {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.PROMOTION_CONTENT_URI, null, PM_ID
						+ "=?", new String[] { Integer.toString(promotionId) },
				null);
		if (cursor != null) {
			boolean isExists = cursor.getCount() > 0;
			if (isExists) {
				while (cursor.moveToNext()) {
					String fileUrl = cursor.getString(cursor
							.getColumnIndex(PM_IMAGE_PATH_LOCAL));
					if (fileUrl == null || fileUrl.length() == 0
							|| !Utility.isFileExists(fileUrl)) {
						deletePromotion(promotionId);
						return false;
					}
				}
				return true;
			}
			cursor.close();
			return false;
		}
		return false;
	}

	public static void updatePromotion(Promotion promotion) {
		ContentValues value = new ContentValues();
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		value.put(PM_ID, promotion.getAnnouncePromoID());
		value.put(PM_YEAR, promotion.getPromoYear());
		value.put(PM_MONTH, promotion.getPromoMonth());
		value.put(PM_CATEGORY, promotion.getSaleCategoryName());
		value.put(PM_IMAGE_NAME, promotion.getPromotionName());
		value.put(PM_IMAGE_PATH, promotion.getPhysicalPath());
		value.put(PM_IMAGE_PATH_LOCAL, promotion.getPhysicalPathLocal());

		resolver.update(EMarketingContentProvider.PROMOTION_CONTENT_URI, value,
				PM_ID + "=?",
				new String[] { Long.toString(promotion.getAnnouncePromoID()) });
	}
}
